package com.etoak.common.jwt;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;
import java.util.Map;

public class MyJwtUtil {

    public static final String KEY_STRING = "qwertyuioplkjhgfdsazxcvbnmqazwsx";

    public static final Key SING_KEY = Keys.hmacShaKeyFor(KEY_STRING.getBytes());

    public static final long EXPIRE = 1000 * 60 * 60 * 12;

    public static String create(Map<String,Object> claimsMap) {
        Date now = new Date();
        return Jwts.builder()
                .signWith(SING_KEY)//签名密钥
                .setClaims(claimsMap)//playoud声明
                .setIssuedAt(now)//签发时间
                .setExpiration(new Date(now.getTime()+ EXPIRE))//过期时间
                .compact();
    }
    /*解析token*/
    public static Map<String,Object> parse(String token) {
        return Jwts.parserBuilder()
                .setSigningKey(SING_KEY)
                .build()
                .parseClaimsJws(token)
                .getBody();
    }
}
